3 use Wikimedia\TestingAccessWrapper
;
10 * @author Katie Filbert < aude.wiki@gmail.com >
12 class SpecialPageTest
extends MediaWikiTestCase
{
14 protected function setUp() {
17 $this->setContentLang( 'en' );
18 $this->setMwGlobals( [
19 'wgScript' => '/index.php',
24 * @dataProvider getTitleForProvider
26 public function testGetTitleFor( $expectedName, $name ) {
27 $title = SpecialPage
::getTitleFor( $name );
28 $expected = Title
::makeTitle( NS_SPECIAL
, $expectedName );
29 $this->assertEquals( $expected, $title );
32 public function getTitleForProvider() {
34 [ 'UserLogin', 'Userlogin' ]
39 * @expectedException PHPUnit_Framework_Error_Notice
41 public function testInvalidGetTitleFor() {
42 $title = SpecialPage
::getTitleFor( 'cat' );
43 $expected = Title
::makeTitle( NS_SPECIAL
, 'Cat' );
44 $this->assertEquals( $expected, $title );
48 * @expectedException PHPUnit_Framework_Error_Notice
49 * @dataProvider getTitleForWithWarningProvider
51 public function testGetTitleForWithWarning( $expected, $name ) {
52 $title = SpecialPage
::getTitleFor( $name );
53 $this->assertEquals( $expected, $title );
56 public function getTitleForWithWarningProvider() {
58 [ Title
::makeTitle( NS_SPECIAL
, 'UserLogin' ), 'UserLogin' ]
63 * @dataProvider requireLoginAnonProvider
65 public function testRequireLoginAnon( $expected, $reason, $title ) {
66 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
68 $user = User
::newFromId( 0 );
69 $specialPage->getContext()->setUser( $user );
70 $specialPage->getContext()->setLanguage( Language
::factory( 'en' ) );
72 $this->setExpectedException( UserNotLoggedIn
::class, $expected );
74 // $specialPage->requireLogin( [ $reason [, $title ] ] )
76 [ $specialPage, 'requireLogin' ],
77 array_filter( [ $reason, $title ] )
81 public function requireLoginAnonProvider() {
84 $expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
85 $expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
88 [ $expected1, null, null ],
89 [ $expected2, 'about', null ],
90 [ $expected2, 'about', 'about' ],
94 public function testRequireLoginNotAnon() {
95 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
97 $user = User
::newFromName( "UTSysop" );
98 $specialPage->getContext()->setUser( $user );
100 $specialPage->requireLogin();
102 // no exception thrown, logged in use can access special page
103 $this->assertTrue( true );
106 public function provideBuildPrevNextNavigation() {
107 yield
[ 0, 20, false, false ];
108 yield
[ 17, 20, false, false ];
109 yield
[ 0, 17, false, false ];
110 yield
[ 0, 20, true, 'Foo' ];
111 yield
[ 17, 20, true, 'Föö_Bär' ];
115 * @dataProvider provideBuildPrevNextNavigation
117 public function testBuildPrevNextNavigation( $offset, $limit, $atEnd, $subPage ) {
118 $this->setUserLang( Language
::factory( 'qqx' ) ); // disable i18n
120 $specialPage = new SpecialPage( 'Watchlist' );
121 $specialPage = TestingAccessWrapper
::newFromObject( $specialPage );
123 $html = $specialPage->buildPrevNextNavigation(
131 $this->assertStringStartsWith( '(viewprevnext:', $html );
133 preg_match_all( '!<a.*?</a>!', $html, $m, PREG_PATTERN_ORDER
);
136 foreach ( $links as $a ) {
138 $this->assertContains( 'Special:Watchlist/' . wfUrlencode( $subPage ), $a );
140 $this->assertContains( 'Special:Watchlist', $a );
141 $this->assertNotContains( 'Special:Watchlist/', $a );
143 $this->assertContains( 'x=25', $a );
149 $this->assertContains(
150 'limit=' . $limit . '&offset=' . max( 0, $offset - $limit ) . '&',
153 $this->assertContains( 'title="(prevn-title: ' . $limit . ')"', $links[$i] );
154 $this->assertContains( 'class="mw-prevlink"', $links[$i] );
155 $this->assertContains( '>(prevn: ' . $limit . ')<', $links[$i] );
160 $this->assertContains(
161 'limit=' . $limit . '&offset=' . ( $offset +
$limit ) . '&',
164 $this->assertContains( 'title="(nextn-title: ' . $limit . ')"', $links[$i] );
165 $this->assertContains( 'class="mw-nextlink"', $links[$i] );
166 $this->assertContains( '>(nextn: ' . $limit . ')<', $links[$i] );
170 $this->assertCount( 5 +
$i, $links );
172 $this->assertContains( 'limit=20&offset=' . $offset, $links[$i] );
173 $this->assertContains( 'title="(shown-title: 20)"', $links[$i] );
174 $this->assertContains( 'class="mw-numlink"', $links[$i] );
175 $this->assertContains( '>20<', $links[$i] );
178 $this->assertContains( 'limit=500&offset=' . $offset, $links[$i] );
179 $this->assertContains( 'title="(shown-title: 500)"', $links[$i] );
180 $this->assertContains( 'class="mw-numlink"', $links[$i] );
181 $this->assertContains( '>500<', $links[$i] );